
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* node = head;
        while (node != nullptr) {
            ListNode* next = node->next;
            node->next = pre;
            pre = node;
            node = next;
        }
        return pre;
    }
};